<template>
  <div>
    <!-- <h1>角色列表1</h1> -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>

      <el-table :data="rolesList" border stripe>
        <!--加一层展开列 type="expand" -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <!-- 一级嵌套循环 -->
            <el-row
              v-for="(item1, i1) in scope.row.children"
              :key="item1.id"
              :class="['bd_botoom', i1 === 0 ? 'bd_top' : '', 'vcenter']"
            >
              <!-- 一行里面有2列 -->
              <el-col :span="5">
                <el-tag
                  closable
                  @close="removeRightById(scope.row, item1.id)"
                  >{{ item1.authName }}</el-tag
                >
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <!-- 此处嵌套循环  渲染二级内容 -->
                <el-row
                  v-for="(item2, i2) in item1.children"
                  :key="item2.id"
                  :class="[i2 === 0 ? '' : 'bd_top', 'vcenter']"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removeRightById(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      type="warning"
                      closable
                      @close="removeRightById(scope.row, item3.id)"
                    >
                      {{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- <pre>{{ scope.row }}</pre> -->
          </template>
        </el-table-column>
        <!--索引列 type="index" -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300" prop="path">
          <template slot-scope="scope">
            <el-button
              @click="roleEdit(scope.row.id)"
              type="primary"
              size="mini"
              >编辑</el-button
            >
            <el-button type="success" size="mini">删除</el-button>
            <el-button
              type="warning"
              size="mini"
              @click="showSetRightDiolog(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 弹出框  分配权限 -->
    <el-dialog
      title="分配权限"
      :visible.sync="showSetRightDiologdata"
      width="50%"
      @close="showRightDiologClose"
    >
      <span>分配权限</span>
      <el-tree
        :data="rightlist"
        :props="treeProps"
        ref="treeRef"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="defkyes"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showSetRightDiolog = false">取 消</el-button>
        <el-button type="primary" @click="alloRight">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      rolesList: [],
      // 控制分配权限的显示隐藏
      showSetRightDiologdata: false,
      // 获取到的权限数据
      rightlist: [],
      //树形控件
      treeProps: {
        label: "authName",
        children: "children",
      },
      // 点√选中trees的节点数组  key值
      defkyes: [],
      // 当前即将分配权限的角色id
      roleId:[]
    };
  },
  created() {
    this.getrolesList();
  },
  computed: {},
  methods: {
    async getrolesList() {
      const { data: res } = await this.$http.get("roles");
      if (res.meta.status !== 200) {
        return this.$message.error("获取权限列表失败");
      }
      this.$message.success("获取数据成功");
      this.rolesList = res.data;
      console.log(this.rolesList);
    },
    roleEdit(id) {
      console.log(id);
      this.$message.success("该功能还未完善,后俩也是哦");
    },
    async removeRightById(row, id) {
      const result = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (result !== "confirm") {
        return this.$message.info("用户取消删除");
      }
      console.log("确认删除");
      const { data: res } = await this.$http.delete(
        `roles/${row.id}/rights/${id}`
      );
      if (res.meta.status !== 200) {
        return this.$message.error("删除失败了");
      }
      // this.getrolesList()  点击删除后会重新刷新页面
      row.children = res.data;
    },
    // 分配权限按钮
    async showSetRightDiolog(role) {
      // 提取到role id  先保存方便后续使用
      this.roleId = role.id
      // 先获取所有权限的数据
      const { data: res } = await this.$http.get("rights/tree");
      if (res.meta.status !== 200) {
        return this.$message.error("获取权限数据失败");
      }
      this.rightlist = res.data;
      console.log(this.rightlist);

      // 递归获取三级节点的id
      this.getLeafKey(role, this.defkyes);

      this.showSetRightDiologdata = true;
    },
    // 通过递归形式 获取角色下所有三级标签  id信息   并保存到数组中
    getLeafKey(node, arr) {
      // 判断第三节点
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach((item) => {
        this.getLeafKey(item, arr);
      });
    },
    // 关闭弹框后 清空3级标签下的id数组
    showRightDiologClose() {
      this.defkyes = [];
      console.log("关闭并清除成功");
    },
    // 点击为角色分配权限
    async alloRight() {
      // 得到要发送请求需要的的半节点  和节点 id  
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys(),
      ];
      console.log(keys);
      // 字符创用逗号拼接一下
      const idStr = keys.join(",");
      console.log(idStr);
      const{data:res} = await this.$http.post(`roles/${this.roleId} /rights`,{rids:idStr});
      if(res.meta.status !== 200){
       return this.$message.error('分配权限失败了')
      }
      this.$message.success("分配权限成功")
      this.getrolesList()
      this.showSetRightDiologdata = false


    },
  },
};
</script>
<style lang="less" scoped>
.el-tag {
  margin: 7px;
}

.bd_top {
  border-top: 1px solid #eee;
}
.bd_botoom {
  border-bottom: 1px solid #eee;
}

.vcenter {
  display: flex;
  align-items: center;
}
</style>
